package com.eibus.applicationconnector.dotnet;


import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;
import com.eibus.xml.nom.Document;
import com.eibus.xml.xpath.XPath;

//@Author	: Lejil
//@Class	: DotnetConnectorTest


/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	nvoke the .NET public Instance & Static Methods For all the dataTypes
 */
public class DotnetConnectorTestCase extends TestCase
{
	Connector  connector ;

	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();
		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}

	protected void tearDown()
	{
	
	}

	public DotnetConnectorTestCase(String name)
	{
		super(name);
	}


	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetConnectorTestCase.class);
	}



	public int  sendRequest(String methodName,String argsOne,String argsTwo)
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod("http://schemas.cordys.com/1.0/dotnet/UnitTestMethods",methodName);
			Node.createTextElement("args1",argsOne,request);
			Node.createTextElement("args2",argsTwo,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- " + methodName +" Exception :- " + ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(request));
		}
		return response;
	}


	public  boolean checkforSoapFault(int response) throws Exception
	{
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode != 0)
			{
				fail(".NET Connector junit Method Failed with SoapFault	:-"+Node.writeToString(faultNode,true));
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}
    
    private void commonTestMethod(String methodName, String argsOne, String argsTwo, String searchXPath, String defaultSearchResult, String expectedResult)
    {
        int response =0;
        try
        {
            response = sendRequest(methodName, argsOne, argsTwo);            
            String result = Node.getDataWithDefault(
                    new XPath(searchXPath).firstMatch(response, null),
                    "Not Found");
            
            if (!result.equalsIgnoreCase(expectedResult))
            {
                //fail("testBooleanType Expected value is True, but returned False");
                System.out.println(methodName);
                System.out.println("Expected value is : " + expectedResult);
                System.out.println("Result value is : " + result);
            }
            assertEquals(expectedResult, result);
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            fail(".NET Connector junit Failed for the .NET Method :- " + methodName);
        }
        finally
        {
            Util.delete(Node.getRoot(response));
        }
    }

	public void testBooleanType() //1
	{
        commonTestMethod("TestBoolType","true","false", ".//TestBoolType/TestBoolType", "False", "True");
	}


	public void testByteType() //2
	{
        commonTestMethod("TestByteType","1","1", ".//TestByteType/TestByteType", "0", "1");
	}


	public void testCharType() //3
	{
        commonTestMethod("TestCharType","a","b", ".//TestCharType/TestCharType","z", "a");
	}

	public void testDoubleType() //4
	{
        commonTestMethod("TestDoubleType","2232324.33312334","24440.233343", ".//TestDoubleType/TestDoubleType","10.01", "2232324.33312334");
	}

	public void testIntegerType() //5
	{
        commonTestMethod("TestIntegerType","9999999","2888", ".//TestIntegerType/TestIntegerType","10.01", "9999999");
	}

	public void testLongType() //6
	{
        commonTestMethod("TestLongType","1234567891234567890","12344", ".//TestLongType/TestLongType","10.01", "1234567891234567890");
	}
    
	public void testShortType() //7
	{
        commonTestMethod("TestShortType","12345","12345", ".//TestShortType/TestShortType","1000", "12345");
	}
    
	public void testSingleType() //8
	{
        commonTestMethod("TestSingleType","12.52","23.52", ".//TestSingleType/TestSingleType","10.20", "12.52");
	}

	public void testStringType() //9
	{
        commonTestMethod("TestStringType","Cordys India Pvt Ltd,Hyderabad","India", ".//TestStringType/TestStringType","10.20", "Cordys India Pvt Ltd,Hyderabad");
	}

	public void testXMLType() //10
	{
        commonTestMethod("TestXmlType","<a>Test</a>","<b>Test</b>", ".//TestXmlType/TestXmlType","<z>Failed</z>", "Test");
	}

	public void testXMLNomType() //11
	{
        int response =0;
        Document document = null;

        try
        {
            document = new Document();
            int node = document.parseString("<location>Hyderabad</location>");
            response = sendRequest("TestXMLNomType",node+"",node+"");
            //String result = Find.firstDataWithDefault(response,"?<TestXMLNomType><TestXMLNomType>","10.20");;
            String result = Node.getDataWithDefault(
                    new XPath(".//TestXMLNomType/TestXMLNomType").firstMatch(response, null),
                    "10.20");
            int dataNode        = Integer.parseInt(result);
            String data     = Node.getDataWithDefault(dataNode,"Falied");
            if (!data.equals("Hyderabad"))
            {
                fail("TestXMLNomType Expected value is -Hyderabad, but returned " + result);
            }
            Util.delete(node);
        }
        catch(Exception ex)
        {
                fail(".NET Connector junit Failed for the Method : testStringType" +ex.toString());
        }
        finally
        {
            Util.delete(Node.getRoot(response));
            document = null;
        }
	}

	public void testDateTimeType() //12
	{
        commonTestMethod("TestDateTimeType","8/23/2004 12:00:00 AM","2004-08-23T00:00:00.00", ".//TestDateTimeType/TestDateTimeType","10.00", "8/23/2004 12:00:00 AM");
	}


	//
	//		STATIC METHODS
	//

	public void testStaticBooleanType() //1
	{
        commonTestMethod("TestStaticBoolType","true","false", ".//TestStaticBoolType/TestStaticBoolType","False", "True");
	}


	public void testStaticByteType() //2
	{
        commonTestMethod("TestStaticByteType","1","1", ".//TestStaticByteType/TestStaticByteType","0", "1");
	}


	public void testStaticCharType() //3
	{
        commonTestMethod("TestStaticCharType","a","b", ".//TestStaticCharType/TestStaticCharType","z", "a");
	}

	public void testStaticDoubleType() //4
	{
        commonTestMethod("TestStaticDoubleType","2232324.33312334","24440.233343", ".//TestStaticDoubleType/TestStaticDoubleType","10.01", "2232324.33312334");
	}

	public void testStaticIntegerType() //5
	{
        commonTestMethod("TestStaticIntegerType","9999999","2888", ".//TestStaticIntegerType/TestStaticIntegerType", "10.01", "9999999");
	}

	public void testStaticLongType() //6
	{
        commonTestMethod("TestStaticLongType","1234567891234567890","12344", ".//TestStaticLongType/TestStaticLongType","10.01", "1234567891234567890");        
	}
    
	public void testStaticShortType() //7
	{
        commonTestMethod("TestStaticShortType","12345","12345", ".//TestStaticShortType/TestStaticShortType","1000", "12345");
	}
    
	public void testStaticSingleType() //8
	{
        commonTestMethod("TestStaticSingleType","12.52","23.52", ".//TestStaticSingleType/TestStaticSingleType","10.20", "12.52");
	}


	public void testStaticStringType() //9
	{
        commonTestMethod("TestStaticStringType","Cordys India Pvt Ltd,Hyderabad","India", ".//TestStaticStringType/TestStaticStringType","10.20", "Cordys India Pvt Ltd,Hyderabad");
	}

	public void testStaticXMLType() //10
	{
        commonTestMethod("TestStaticXmlType","<a>Test</a>","<b>Test</b>", ".//TestStaticXmlType/TestStaticXmlType","<z>Failed</z>", "Test");
	}

	public void testStaticXMLNomType() //11
	{
        int response =0;
        Document document = null;

        try
        {
            document = new Document();
            int node = document.parseString("<location>Hyderabad</location>");
            response = sendRequest("TestStaticXMLNomType",node+"",node+"");
            //String result = Find.firstDataWithDefault(response,"?<TestStaticXMLNomType><TestStaticXMLNomType>","10.20");
            String result = Node.getDataWithDefault(
                    new XPath(".//TestStaticXMLNomType/TestStaticXMLNomType").firstMatch(response, null),
                    "10.20");
            int dataNode        = Integer.parseInt(result);
            String data     = Node.getDataWithDefault(dataNode,"Falied");
            if (!data.equals("Hyderabad"))
            {
                fail("TestXMLNomType Expected value is -<location>Hyderabad</location>, but returned " + result);
            }
            Util.delete(node);
        }
        catch(Exception ex)
        {
                fail(".NET Connector junit Failed for the Method : testStringType" +ex.toString());
        }
        finally
        {
            Util.delete(Node.getRoot(response));
            document = null;
        }
	}

	public void testStaticDateTimeType() //12
	{
        commonTestMethod("TestStaticDateTimeType","8/23/2004 12:00:00 AM","2004-08-23T00:00:00.00", ".//TestStaticDateTimeType/TestStaticDateTimeType","10.20", "8/23/2004 12:00:00 AM");
	}
}
